package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    /**
     * 插入一条订单数据
     * @param orders
     */
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into sky_take_out.orders (number, status, user_id, address_book_id, order_time, " +
            "checkout_time, pay_method, pay_status, amount, remark, phone, address, user_name, consignee," +
            " cancel_reason, rejection_reason, cancel_time, estimated_delivery_time," +
            "delivery_status, delivery_time, pack_amount, tableware_number, tableware_status) VALUES " +
            "(#{number}, #{status}, #{userId}, #{addressBookId}, #{orderTime}," +
            " #{checkoutTime}, #{payMethod}, #{payStatus}, #{amount}, #{remark}, #{phone}, " +
            "#{address}, #{userName}, #{consignee}, #{cancelReason}, #{rejectionReason}, " +
            "#{cancelTime}, #{estimatedDeliveryTime}, #{deliveryStatus}, #{deliveryTime}, " +
            "#{packAmount}, #{tablewareNumber},#{tablewareStatus})")
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 历史订单分页查询
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据套餐id查询套餐
     * @param id
     * @return
     */
    @Select("select * from orders where id=#{id}")
    Orders getOrderById(Long id);

    /**
     * 各个状态的订单数量统计
     * @param deliveryInProgress
     * @return
     */
    @Select("select count(id) from orders where status=#{status}")
    Integer countStatus(Integer deliveryInProgress);

    /**
     * 根据订单状态查询超时订单
     * @param pendingPayment
     * @param payTime
     * @return
     */
    @Select("select * from orders where status=#{pendingPayment} and order_time<=#{payTime}")
    List<Orders> getOrderByPayTime(Integer pendingPayment, LocalDateTime payTime);

    /**
     * 查询配送中的订单
     * @param deliveryInProgress
     * @return
     */
    @Select("select * from orders where status=#{deliveryInProgress}")
    List<Orders> getOrderByDelivery(Integer deliveryInProgress);

    /**
     * 根据动态条件统计订单数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);

    /**
     * 根据动态条件统计营业额数据
     * @param map
     * @return
     */
    Double sumByMap(Map map);
}
